// pages/common/live/live.js
//packageStore/pages/market/live/live.js
import {
  activities,
  liveService
} from '../../../../js/modules/market';
import BasePage from '../../../../js/common/base-page'
BasePage({
  validateLogin:false,
  /**
   * 页面的初始数据
   */
  data: {
    list: [
      // { pic: 'http://img4.imgtn.bdimg.com/it/u=3039320892,1295927504&fm=26&gp=0.jpg', status: '未开始', time: '02-25 20:00' },
      // { pic: 'http://img4.imgtn.bdimg.com/it/u=3039320892,1295927504&fm=26&gp=0.jpg', status: '已开始', time: '02-25 20:00' },
      // { pic: 'http://img4.imgtn.bdimg.com/it/u=3039320892,1295927504&fm=26&gp=0.jpg', status: '已结束', time: '02-25 20:00' },
    ],
    pageIndex:1,
    pageSize:10,
    hasMoreData: true,
    isLoadComplete: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData();
  },
  //获取数据
  getData:function(){
    var that = this;
    let {pageIndex,pageSize,list} = that.data;
    let args = {
      pageIndex:pageIndex,
      pageSize:pageSize
    }
    liveService.getPage(args, function(res) {
      var count = res.count;
      var data = res.data;
      var hasMoreData = count > pageIndex * pageSize;
      //当分页大于1时，累加
      if (pageIndex > 1) {
        data = list.concat(data);
      };
      that.liveStatus(data)
      that.setData({
        list: data,
        hasMoreData:hasMoreData,
        isLoadComplete: true
      })
    });
  },
  liveStatus: function(data) {
    for (var i = 0; i < data.length; i++) {
      switch (data[i].Status) {
        case "0":
          data[i].Status = "未知";
          break
        case "1":
          data[i].Status = "即将开始";
          break
        case "2":
          data[i].Status = "直播中";
          break
        case "3":
          data[i].Status = "暂停中";
          break
        case "4":
          data[i].Status = "网络异常";
          break
        case "5":
          data[i].Status = "禁播";
          break
        case "6":
          data[i].Status = "已结束";
          break
        case "7":
          data[i].Status = "已过期";
          break
      }
    }
  },

  goToLive: function (e) {
    var roomid = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomid}`
    })
    requirePlugin('live-player-plugin').getLiveStatus({
      room_id: roomid
    })
      .then(res => {
        // 101: 直播中, 102: 未开始, 103: 已结束, 104: 禁播, 105: 暂停中, 106: 异常，107：已过期 
        const liveStatus = res.liveStatus
        let data={
          id: roomid,
          status: liveStatus
        }
        liveService.updateLiveStatus(data)
      })
      .catch(err => {
        console.log('get live status', err)
      })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.setData({
      pageIndex: 1,
      isLoadComplete: false,
    })
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.hasMoreData) {
      var index = this.data.pageIndex;
      this.data.pageIndex = index + 1;
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})